<?php
/**
 * Author: DRERY <rui1642@foxmail.com>
 */
namespace backend\components\widgets;

use yii\bootstrap\Widget;
use yii\helpers\Html;

class PopoverFull extends Widget
{
    public $tagName = 'span';

    public $content;

    public $popoverContent;

    public $class = 'nowrap-text';

    public $overflowHidden = true;

    public $width = '300px';

    public $popoverCss;

    public function init()
    {
        parent::init();
        $this->clientOptions = false;
        !$this->popoverContent && $this->popoverContent = $this->content;
        $this->options = array_merge($this->options, ['class' => $this->class, 'data-toggle' => 'popover', 'data-content' => $this->popoverContent, 'data-placement' => 'top', 'data-trigger' => 'hover']);
    }

    public function run()
    {
        $view = $this->getView();

        $class = $this->class;
        if ($this->overflowHidden) {
            $tagCss = <<<CSS
.{$class}{
    display: block;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    max-width: {$this->width};
}
.popover{
    max-width: {$this->width};    
}
.popover-content{
    max-width: {$this->width};
    word-wrap : break-word;
}
CSS;
            $view->registerCss($tagCss);
        }

        if (!empty($this->popoverCss)) {
            $popoverCss = $this->popoverCss;
            foreach ($popoverCss as $key => $value) {
                $popoverCssList[] = "$key: $value;";
            }
            $popoverCss = join("\n", $popoverCssList);
            $css = <<<CSS
.popover-content{
$popoverCss
}
CSS;
            $view->registerCss($css);
        }

        $js = <<<JS
$('.{$class}').popover();
JS;
        $view->registerJs($js);
        return Html::tag($this->tagName, $this->content, $this->options);
    }
}